<script setup lang="ts">
interface Props {
    list: Array<{ title: string, [key: string]: any }>
    active: number
    type?: number
}

withDefaults(defineProps<Props>(), {
    type: 1,
})

const emit = defineEmits(['update:active'])


function handlePressDown($event: any) {
    $event.target.style.transition = 'transform .1s'
    $event.target.style.transform = 'translateY(2px)'
}

function handlePressUp($event: any) {
    $event.target.style.transform = ''
}
</script>

<template>
    <div class="tabs-title tabs-title-2 scroll-x hide-scroll   flex">
        <button
            v-for="item, index in list"
            :key="item.title" class="tabs-btn tabs-btn-2 btn-like cursor-pointer"
            :class="{ active: index === active }"
            @click="emit('update:active', index)" @mousedown="handlePressDown($event)" @mouseup="handlePressUp($event)"
            @touchstart="handlePressDown($event)" @touchend="handlePressUp($event)">
            {{ item.title }}
        </button>
        <div class="tabs-indicator" />
    </div>
</template>

<style>
:root {
    --tabs-width: 0;
    --tabs-indicator-position: 0;
    --tabs-icon-size: 18px;
    --tabs-padding: 0 0.5rem 0 0.5rem;
    --tabs-border-radius: 0rem;
    --tabs-active-border: none;
    --tabs-title-color: #b3bec1;
    --tabs-btn-bg-color: #333738;
}
</style>

<style scoped lang="scss">
.btn-like {
    -webkit-user-select: none;
    -moz-user-select: none;
    user-select: none;
    touch-action: manipulation;
    padding: var(--tabs-padding);
    border-radius: var(--tabs-border-radius);
    border: var(--tabs-border);
    color: var(--tabs-title-color);
    background-color: var(--tabs-btn-bg-color);
}

.tabs-title {
    position: relative;
    display: flex;
    height: 2.5rem;
    border-radius: var(--tabs-border-radius);
    color: var(--tabs-title-color);
    overflow: hidden;
}

.tabs-btn {
    flex: 1;
    align-self: stretch;
    text-wrap: nowrap;
    padding: var(--tabs-padding);
    display: flex;
    justify-content: center;
    align-items: center;

    &-2 {
        &.active {
            color: #ffffff;
            font-weight: 800;
        }
    }
}

.tabs-indicator {
    position: absolute;
    bottom: 0;
    left: 0;
    height: 0.15rem;
    width: var(--tabs-width);
    background-color: #2cd97d;
    transition: transform 0.3s;
    transform: translate(var(--tabs-indicator-position));
}

.bg-tab_selected {
    background-color: #3b4142;
    color: #ffffff;
    font-weight: 800;
}
</style>
